#include "EditorBreakpointAbilityView.h"

#include "EditorBreakpointAbilityViewModel.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"  

#include "Widgets/Positioners/VerticalPositionerWidget.h"
#include "Widgets/CanvasWidget.h"
#include "Widgets/EditorCheckBoxWidget.h"
#include "Widgets/TextWidget.h"
#include "Widgets/RectangleWidget.h"

void REditorBreakpointAbilityView::CreateWidget(TSharedObjectPtr<RWidget> InParent)
{

    auto Root =NewWidget<RWidget>(InParent);
    Root->Anchor.Fill(InParent);

    List = NewWidget<RVerticalPositionerWidget>(Root);
    List->Anchor.HorizontalFill(Root);
    List->Anchor.TopReference=Root;
    List->Anchor.TopOffset=5;
    List->Anchor.BottomReference=Root;

    BreakpointViewModel->OnBreakpointAdded.Bind([this]()
    {
        UpdateBreakpointList();
    });

    BreakpointViewModel->OnBreakpointRemoved.Bind([this]()
    {
        UpdateBreakpointList();
    });

    UpdateBreakpointList();

}

void REditorBreakpointAbilityView::BindViewModel(TSharedObjectPtr<REditorAbilityViewModel> InViewModel)
{
    Super::BindViewModel(InViewModel);
    BreakpointViewModel = InViewModel;
}

void REditorBreakpointAbilityView::FillOperations(TSharedObjectPtr<RWidget> InTitleBar)
{
}

void REditorBreakpointAbilityView::UpdateBreakpointList()
{
    List->ClearSubWidgets();

    auto BreakpointNumber = BreakpointViewModel->GetBreakpointNumber();
    for (int64_t Index = 0; Index < BreakpointNumber; ++Index)
    {
      
        

        auto BreakpointWidget = NewWidget<RWidget>(List);
        BreakpointWidget->Anchor.HorizontalFill(List);
        BreakpointWidget->Height = BreakpointViewModel->LineHeight;

        auto BreakpointIcon = NewWidget<RCanvasWidget>(BreakpointWidget);
        BreakpointIcon->Width=BreakpointViewModel->LineHeight;
        BreakpointIcon->X=5;
        BreakpointIcon->Height=BreakpointViewModel->LineHeight;
        BreakpointIcon->Anchor.VerticalCenterReference=BreakpointWidget;
        RCanvasWidgetWeakPtr BreakpointIconWeakPtr = BreakpointIcon;
        BreakpointIcon->OnPaint.BindLambda([this,BreakpointIconWeakPtr,Index]()
        {
            BreakpointIconWeakPtr->Clear();

            BreakpointIconWeakPtr->AddCircle(
                SVec2(BreakpointViewModel->LineHeight*0.5,BreakpointViewModel->LineHeight*0.5),
                5,
                BreakpointViewModel->GetBreakpointColor(Index)
            );
        });

        auto CheckBox = NewWidget<REditorCheckBoxWidget>(BreakpointWidget);
        CheckBox->Anchor.VerticalCenterReference=BreakpointWidget;
        CheckBox->Anchor.LeftReference=BreakpointIcon;
        CheckBox->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
        CheckBox->Anchor.LeftOffset=5;
        CheckBox->Height=BreakpointViewModel->LineHeight;
        CheckBox->Width=BreakpointViewModel->LineHeight;
        CheckBox->CheckState= [this,Index]()
        {
            return BreakpointViewModel->IsBreakpointEnabled(Index) ? ECheckBoxWidgetState::Checked : ECheckBoxWidgetState::Unchecked;
        };
        CheckBox->OnCheckStateChanged=[this,Index](ECheckBoxWidgetState)
        {
            BreakpointViewModel->TogglerBreakpointEnabled(Index);
        };

        auto FileName = NewWidget<RTextWidget>(BreakpointWidget);
        FileName->Anchor.LeftReference=CheckBox;
        FileName->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
        FileName->Anchor.LeftOffset=5;
        FileName->Text=[this,Index]()
        {  
            auto BreakpointFileName = BreakpointViewModel->GetBreakpointFileName(Index);
            auto BreakpointFilePath = BreakpointViewModel->GetBreakpointFilePath(Index);
        
            return BreakpointFileName+U"  "+BreakpointFilePath;
        };
        FileName->VerticalAlignment=ETextVerticalAlignment::Center;

        auto Line = NewWidget<RRectangleWidget>(BreakpointWidget);
        Line->Height= BreakpointViewModel->LineHeight;
        Line->Anchor.RightReference=BreakpointWidget;
        Line->Anchor.RightOffset= 5;
        Line->Radius=BreakpointViewModel->LineHeight*0.5;
        Line->FillColor=REditor::Get()->CurrentTheme->TextBackgroundColor;

        FileName->Anchor.RightReference=Line;
        FileName->Anchor.RightOffset=5;
        FileName->Anchor.RightReferenceEdge=EHorizontalWidgetEdge::Left;

        auto LineText = NewWidget<RTextWidget>(Line);
        LineText->Anchor.CenterIn(Line);
        LineText->Text=[this,Index]()
        {
            return CString(BreakpointViewModel->GetBreakpointLine(Index)+1);
        };
        LineText->VerticalAlignment=ETextVerticalAlignment::Center;
        LineText->HorizontalAlignment=ETextHorizontalAlignment::Center;

        Line->Width=[LineText]()
        {
            return LineText->GetTextSize().X+10;
        };
        
    }
}
